package java8;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import domain.Student;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 描述
 * yanglh
 * 2017/6/28 0028 8:09
 */
public class StreamStudy {
    public static void main(String[] args) {
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(1);
        jsonArray.add(2);
        jsonArray.add(3);
        List list = jsonArray;
        System.out.println(list);
        JSONObject jsonObject = new JSONObject();
        Map map = jsonObject;
        System.out.println(map);
    }

    public static void mapNewFun(){
        Map<Integer, String> HOSTING = new HashMap<>();
        HOSTING.put(1, null);
        HOSTING.put(2, "heroku.com");
        HOSTING.put(3, "digitalocean.com");
        HOSTING.put(4, "aws.amazon.com");
        HOSTING.remove(2,"heroku.com");
        System.out.println(HOSTING.putIfAbsent(3,"luchan"));
        System.out.println(HOSTING);
    }

    public static void collects(){
        System.out.println(Stream.of("a", "b", "c").collect(Collectors.joining(",")));
    }

    public static void mapFilter(){
            Map<Integer, String> HOSTING = new HashMap<>();
            HOSTING.put(1, null);
            HOSTING.put(2, "heroku.com");
            HOSTING.put(3, "digitalocean.com");
            HOSTING.put(4, "aws.amazon.com");

            System.out.println(HOSTING.get(1));

            //Map -> Stream -> Filter -> Map
            Map<Integer, String> collect = HOSTING.entrySet().stream()
                    .filter(k -> HOSTING.get(k.getKey()) != null)
                    .collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));

            System.out.println(collect); //output : {2=heroku.com}
    }

    public static void listFun(){
        List<String> testList = new ArrayList<>();
        testList.add("2111");
        testList.add("2232");
        testList.add("133222");
        testList.add("233432");
        testList.removeIf(test->test.startsWith("1"));
        System.out.println(testList);
    }
}
